<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet 
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        exclude-result-prefixes="xsl xsi"
>

    <xsl:output 
        method="xml" 
        indent="yes"
        encoding="ISO-8859-1"
	omit-xml-declaration="yes"
    />
    
    <xsl:variable name="rankImagePrefix">/images/bf2s/rank_</xsl:variable>
    <xsl:variable name="rankImageSuffix">.png</xsl:variable>
    <xsl:variable name="rankImageWidth">16</xsl:variable>
    <xsl:variable name="rankImageHeight">16</xsl:variable>

    <xsl:template match="players">
        <!-- <h2 class="pagetitle">BF2 Leaderboard</h2> -->
        <table width="740" class="zebra-table">
            <tr>
                <th>Nick</th>
                <th>Rank</th>
                <th>Score</th>
                <th>Kills</th>
                <th>Deaths</th>
                <th>Wins</th>
                <th>Losses</th>
                <th>K/D</th>
                <th>SPM</th>
                <th>Time</th>
            </tr>
            <xsl:apply-templates select="player"/>
        </table>
    </xsl:template>
    
    <xsl:template match="player">
        <tr>
            <xsl:if test="position() mod 2 != 0">
                <xsl:attribute name="class">odd</xsl:attribute>
            </xsl:if>
            <td>
                <a>
                    <xsl:attribute name="href">
                        <xsl:value-of select="@link"/>
                    </xsl:attribute>
                    <xsl:call-template name="rankimage">
                        <xsl:with-param name="rank">
                            <xsl:value-of select="@rank"/>
                        </xsl:with-param>
                    </xsl:call-template>
                    <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
                    <xsl:value-of select="@nick"/>
                </a>
            </td>
            <td align="center">
                <xsl:call-template name="rankname">
                    <xsl:with-param name="rank">
                        <xsl:value-of select="@rank"/>
                    </xsl:with-param>
                </xsl:call-template>
            </td>
            <td align="right">
                <xsl:value-of select="@score"/>
            </td>
            <td align="right">
                <xsl:value-of select="@kills"/>
            </td>
            <td align="right">
                <xsl:value-of select="@deaths"/>
            </td>
            <td align="right">
                <xsl:value-of select="@wins"/>
            </td>
            <td align="right">
                <xsl:value-of select="@losses"/>
            </td>
            <td align="right">
                <xsl:value-of select="round((@kills div @deaths) * 100) div 100"/>
            </td>
            <td align="right">
                <xsl:value-of select="round((@score div (@time div 60)) * 100) div 100"/>
            </td>
            <td align="right">
                <xsl:value-of select="round((@time div 3600) * 100) div 100"/>
		<xsl:text>h</xsl:text>
            </td>
        </tr>
    </xsl:template>
    
    <xsl:template name="rankname">
        <xsl:param name="rank"/>
        <xsl:choose>
            <xsl:when test='$rank="0"'>Private</xsl:when>
            <xsl:when test='$rank="1"'>Private First Class</xsl:when>
            <xsl:when test='$rank="2"'>Lance Corporal</xsl:when>
            <xsl:when test='$rank="3"'>Corporal</xsl:when>
            <xsl:when test='$rank="4"'>Sergeant</xsl:when>
            <xsl:when test='$rank="5"'>Staff Sergeant</xsl:when>
            <xsl:when test='$rank="6"'>Gunnery Sergeant</xsl:when>
            <xsl:when test='$rank="7"'>Master Sergeant</xsl:when>
            <xsl:when test='$rank="8"'>First Sergeant</xsl:when>
            <xsl:when test='$rank="9"'>Master Gunnery Sergeant</xsl:when>
            <xsl:when test='$rank="10"'>Sergeant Major</xsl:when>
            <xsl:when test='$rank="11"'>Sergeant Major of the Corps</xsl:when>
            <xsl:otherwise><xsl:value-of select="$rank"/></xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="rankimage">
        <xsl:param name="rank"/>
        <img border="0">
            <xsl:attribute name="src">
                <xsl:value-of select="$rankImagePrefix"/>
                <xsl:value-of select="$rank"/>
                <xsl:value-of select="$rankImageSuffix"/>
            </xsl:attribute>
            <xsl:attribute name="width">
                <xsl:value-of select="$rankImageWidth"/>
            </xsl:attribute>
            <xsl:attribute name="height">
                <xsl:value-of select="$rankImageHeight"/>
            </xsl:attribute>
            <xsl:attribute name="alt">
                <xsl:call-template name="rankname">
                    <xsl:with-param name="rank">
                        <xsl:value-of select="$rank"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:attribute>
        </img>
    </xsl:template>
    
</xsl:stylesheet>
